{# Make sure to avoid 4-spaces-deep (or deeper) indentation #}

{# The results get of Jinja expansion get fed into Markdown, which #}
{# may do undesired things with such deeply indented content. #}

{% macro youtube(id) -%}
  <iframe width="480" height="400" src="//www.youtube.com/embed/{{id}}" frameborder="0" allowfullscreen>
  </iframe>
{%- endmacro %}

{% macro youku(id) -%}
<div class="row"><div class="embed-responsive embed-responsive-16by9 col-md-8"><iframe class="embed-responsive-item" src="http://player.youku.com/embed/{{id}}" allowfullscreen></iframe></div><div class="col-md-4"></div></div>
{%- endmacro %}

{% macro collapsible(title, id="collapsery") -%}
<div class="panel panel-default" markdown="block"><div class="panel-heading" markdown="span">
  <h4 class="panel-title" markdown="span">
  <a data-toggle="collapse" href="#{{ id }}" markdown="span">
  {{ title }}
  <span style="font-size:x-small">(click to view)</span>
  </a>
  </h4>
  </div>

  <div id="{{ id }}" class="panel-collapse collapse" markdown="block">
   <div class="panel-body" markdown="block">
   {{ caller() }}
   </div>
  </div>
</div>
{%- endmacro %}

{% macro accordion(title, id="accordion", subtitle="", sub_color="") -%}
<div class="panel panel-default" style="margin-bottom:0" markdown="block"><div class="panel-heading" >
<h3 class="panel-title"><a data-toggle="collapse" href="#{{ id }}_accordion" >{{ title }}
{% if subtitle %}<span style="font-size:x-small {% if sub_color %}; color:{{ sub_color }}{% endif %}">({{ subtitle }})</span>{% endif %}</a>
</h3></div>
  <div id="{{ id }}_accordion" class="panel-collapse collapse" markdown="block">
   <div class="panel-body" markdown="block">
   {{ caller() }}
   </div>
  </div>
</div>
{% raw %}
<script>
$('p').each(function () {if($(this).html()==""){$(this).remove()}});
</script>
{% endraw %}
{%- endmacro %}

{% macro button(url, text="Go <i class='fa fa-chevron-right'></i>", type="primary", size="sm", left_fa_icon="", right_fa_icon="") -%}
  <a href="{{ url }}" role="button" class="btn btn-{{ type }} btn-{{ size }}">
  {%- if left_fa_icon %}
  <i class="fa fa-{{ left_fa_icon }}></i>
  {% endif -%}
  {%- if right_fa_icon %}
  <i class="fa fa-{{ right_fa_icon }}></i>
  {% endif -%}
  {{ text }}</a>
{%- endmacro %}

{% macro file(url, dlname) -%}
<i class="fa" id="for_download_file_{{ dlname }}"></i> {{ dlname }} <a href="{{ url }}" download="{{ dlname }}"><i class='fa fa-download'></i></a>
{%- endmacro %}

{% macro mailto(email) -%}
&middot; <a href="mailto:{{ email }}"><i class="fa fa-envelope-o"></i></a> &middot; {{ email }}
{%- endmacro %}

{% macro file_download(dir, filename, dlname="") -%}
<i class="fa" id="for_download_file_{{ filename }}"></i> {% if dlname %} {{ dlname }} {% else %} {{ filename }} {% endif %} <a href="{{ dir }}/{{filename}}" {% if dlname %} download="{{ dlname }}" {% endif %}><i class='fa fa-download'></i></a>
{%- endmacro %}

{% macro grace_rules(week) -%}
rules:
  start:
    -
      if_before: end_week {{ week }}
      if_has_role: [student, ta, instructor]
      if_has_fewer_sessions_than: 2
      may_start_new_session: True
      may_list_existing_sessions: True

    -
      may_start_new_session: False
      may_list_existing_sessions: True

  access:
     -
       if_before: end_week {{ week + 1}}
       permissions: [view, submit_answer, end_session, see_correctness]

     -
       permissions: [view, submit_answer, end_session, see_correctness, see_answer]

  grading:
    -
      if_completed_before: end_week {{ week }}
      grade_identifier: la_quiz
      grade_aggregation_strategy: max_grade
      credit_percent: 100

    -
      if_completed_before: end_week {{ week + 1 }}
      grade_identifier: la_quiz
      grade_aggregation_strategy: max_grade
      credit_percent: 50

    -
      grade_identifier: null
{%- endmacro %}


{% macro download_view_pdf(filepath, file_id, dlname) -%}
<i class="fa fa-file-pdf-o"></i> {{ dlname}} <a href="{{ filepath }}" id="{{ file_id }}_download_link" download="{{ dlname }}" title="下载"> &middot; <i class="fa fa-download"></i></a>
&middot; 
<a href="#" onclick="multi_embed_viewer(this)" id="{{ file_id }}" title="在线查看"><i class="fa fa-eye"></i></a>
<div id="{{ file_id }}_pdfviewer_div">
</div>
{%- endmacro %}



{% macro downloadviewpdf(filepath, dlname="") -%}
<i class="fa fa-file-pdf-o"></i> {{ dlname}} <a href="{{ filepath }}" class="file_download_view" download="{{ dlname }}" title="下载"><i class="fa fa-download"></i></a>
{%- endmacro %}

{% macro pdfjs() -%}
{% block javascript %}
{% raw %}
<script type="text/javascript">
$( document ).ready(generate_download_pdf_view());
function generate_download_pdf_view(){
var all_li=$(".file_download_view");
for(var i=0; i<all_li.length; i++){
var element_i = all_li[i]
var url_i = $(element_i).attr("href");
var file_id = get_file_name(url_i);
//console.log(file_id);
if($(file_id).length == 0){
$('<a href="#" onclick="embed_viewer(this)" id="' + file_id +'"><i class='fa fa-eye'></i>在线查看</a> <div id="' + file_id +'_pdfviewer_div"></div>').insertAfter($(element_i));            
$(element_i)
    .attr("id", file_id+"_download_link")
    .after(" &middot; ");}
}
}
function get_file_name(url){
var filename = url.substring(url.lastIndexOf('/')+1).replace(/\.[^/.]+$/, "");
return filename;
}

function embed_viewer(item)
{
// alert on browser which is below IE 9
var div = document.createElement("div");
div.innerHTML = "<!--[if lt IE 9]><i></i><![endif]-->";
var isIeLessThan9 = (div.getElementsByTagName("i").length == 1);
if (isIeLessThan9) {
alert("ie10以下的版本的浏览器不支持预览");
}
else
{

var hrefID = item.id + "_download_link";              
var viewpath = $("#"+hrefID).attr("href");
//var filename = viewpath.substring(viewpath.lastIndexOf('/')+1);
var display_DIV_ID = item.id + "_pdfviewer_div";

//alert(href);
$("#" + display_DIV_ID).html(
"<iframe src = '/static/pdf.js/web/viewer.html?file=" + viewpath + "' width='600' height='450' allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe>"
);
$('html, body').animate({
scrollTop: $(item).offset().top
}, 1000);
$(item).attr("onclick","close_viewer(this);").html("<i class='fa fa-eye-slash'></i>关闭在线查看");
}
}
function close_viewer(item)
{
var display_DIV_ID = item.id + "_pdfviewer_div";
$("#" + display_DIV_ID).html("");
//$("#" + display_DIV_ID).slideUp("slow", function(){ $("#" + display_DIV_ID).html("");});
//$("#" + display_DIV_ID).attr("style", "")
$(item).attr("onclick","embed_viewer(this)").html("<i class='fa fa-eye'></i>在线查看");
$('html, body').animate({
scrollTop: $(item).offset().top
}, 1000);
}



</script>
{% endraw %}
{% endblock %}

{%- endmacro %}